/*	BuildComsat class definition.
 *
*/

#include "BuildingPlacer.h"
#include "BuildComsat.h"

void BuildComsat::Enter()
{
	DEBUG_PRINT("BuildComsat(%p) %s entering\n", this, GetName().c_str());

}

void BuildComsat::Execute()
{
	DEBUG_PRINT("BuildComsat(%p) %s executing\n", this, GetName().c_str());
	if(ccToAddTo)
	{
		ccToAddTo->buildAddon(BWAPI::UnitTypes::Terran_Comsat_Station);
		BB->SpendResources(BWAPI::UnitTypes::Terran_Comsat_Station);
	}
	control->FinishWithSuccess();
}

void BuildComsat::Exit()
{
	DEBUG_PRINT("BuildComsat(%p) %s exiting\n", this, GetName().c_str());
	ccToAddTo = NULL;
	
}

bool BuildComsat::CheckConditions()
{
	DEBUG_PRINT("BuildComsat(%p) %s checking conditions\n", this, GetName().c_str());
	
	std::list<BWAPI::Unit*>* cc = BB->GetBuildingsByTypeGeneral(BWAPI::UnitTypes::Terran_Command_Center);
	std::list<BWAPI::Unit*>* com = BB->GetBuildingsByTypeGeneral(BWAPI::UnitTypes::Terran_Comsat_Station);
	if(cc)
	{
		for(std::list<BWAPI::Unit*>::iterator it = cc->begin(); it != cc->end(); it++)
		{
			if(!(*it)->getAddon())
				ccToAddTo = *it;
		}
		if(BB->isBuildBuilding(BWAPI::UnitTypes::Terran_Comsat_Station) )
		{
			DEBUG_PRINT("BuildComsat(%p) %s checked conditions and has %d minerals to spend\n", this, GetName().c_str(), BB->GetMineralsToSpend());
			return true;
		}
	}
	

	return false;
}